﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace StreetSmartAdmin
{
    public partial class FormContent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //((Label)Header.FindControl("lblUser")).Text = ((Userinfo)Session["UserInfo"]).fullname; 
            //((Label)Header.FindControl("lblUser")).Text = ((String)Session["UserName"]);
            ((Label)Header.FindControl("lblUser")).Text = (User.Identity.Name);
            if (!validation(UserCap(User.Identity.Name)))
            {
                MessageBox("Sorry, you don't have permission to this page!");
                Response.Redirect("login.aspx");
            }

        }

        private void MessageBox(string message)
        {
            if (!string.IsNullOrEmpty(message))
            {
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alt1", "alert('" + message + "');", true);
            }
        }

        private bool validation(string AdminName)
        {
            bool boolReturnValue = false;
            System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];
            string connectionString = connString.ConnectionString;
            SqlConnection Connection = new SqlConnection(connectionString);
            String strSQL = "Select * From Users;";
            SqlCommand command = new SqlCommand(strSQL, Connection);
            SqlDataReader Dr;
            Connection.Open();
            Dr = command.ExecuteReader();
            while (Dr.Read())
            {
                if ((AdminName == (Dr["UserName"].ToString())) && (((int)Dr["Permission"]<2)))
                {
                    boolReturnValue = true;
                    Dr.Close();
                    return boolReturnValue;
                }
            }
            return boolReturnValue;
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue == "AccountStatus")
            {
                MultiView1.ActiveViewIndex = 1;
            }
            else if (DropDownList1.SelectedValue == "PromotionCode")
            {
                MultiView1.ActiveViewIndex = 2;
            }
            else if (DropDownList1.SelectedValue == "SourceCodeRESI")
            {
                MultiView1.ActiveViewIndex = 3;
            }
            else if (DropDownList1.SelectedValue == "SourceCodeSME")
            {
                MultiView1.ActiveViewIndex = 4;
            }
            else if (DropDownList1.SelectedValue == "ProductTypeRESI")
            {
                MultiView1.ActiveViewIndex = 5;
            }
            else if (DropDownList1.SelectedValue == "ProductTypeSME")
            {
                MultiView1.ActiveViewIndex = 6;
            }
            else if (DropDownList1.SelectedValue == "Operator")
            {
                MultiView1.ActiveViewIndex = 7;
            }
            else
            {
                MultiView1.ActiveViewIndex = 0;
            }
        }

        protected void insertRecord(string table, string field, string value)
        {
            string insertcommand = "insert into " + table + " (" + field + ") values('" + value + "');";
            string connectionString = null;
            System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];
            if (null != connString)
                connectionString = connString.ConnectionString;
            System.Data.SqlClient.SqlConnection connection = null;
            connection = new System.Data.SqlClient.SqlConnection(connectionString);
            connection.Open();
            System.Data.SqlClient.SqlCommand command;
            command = connection.CreateCommand();
            command.CommandText = insertcommand;
            command.ExecuteNonQuery();
            connection.Close();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!(TextBox1.Text == ""))
            {
                insertRecord("tblAccountStatus", "AccountStatusName", TextBox1.Text);
                GridView1.DataBind();
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (!(TextBox2.Text == ""))
            {
                insertRecord("tblPromotionCode", "PromotionCodeName", TextBox2.Text);
                GridView2.DataBind();
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            if (!(TextBox3.Text == ""))
            {
                insertRecord("SourceCodeRESI", "SourceCodeRESI_Name", TextBox3.Text);
                GridView3.DataBind();
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            if (!(TextBox4.Text == ""))
            {
                insertRecord("SourceCodeSME", "SourceCodeSME_Name", TextBox4.Text);
                GridView4.DataBind();
            }
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            if (!(TextBox5.Text == ""))
            {
                insertRecord("ProductTypeRESI", "ProductTypeRESI_Name", TextBox5.Text);
                GridView5.DataBind();
            }
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            if (!(TextBox6.Text == ""))
            {
                insertRecord("ProductTypeSME", "ProductTypeSME_Name", TextBox6.Text);
                GridView6.DataBind();
            }
        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            if (!(TextBox7.Text == ""))
            {
                insertRecord("tblOperator", "OperatorName", TextBox7.Text);
                GridView7.DataBind();
            }
        }

        private string UserCap(string username)
        {
            string str1 = username.Substring(0, 1);
            string str2 = username.Substring(1, username.Length - 1);
            return str1.ToUpper() + str2;
        }
    }
}